#!/bin/bash

PROG=$0

kmoddir=extra

while getopts "n:v:f:k:" opt; do
	case $opt in
		n) pkgname=$OPTARG ;;
		v) pkgver=$OPTARG  ;;
		f) filename=$OPTARG ;;
		k) kmoddir=$OPTARG ;;
	esac
done

if [ -z "${pkgname}" -o -z "${pkgver}" -o -z "${filename}" ]; then
	echo "Usage: $PROG -n <pkgname> -v <pkgver> [-k <kmoddir> ] -f <filename>"
	exit 1
fi

cat >${filename} <<EOF
PACKAGE_NAME="${pkgname}"
PACKAGE_VERSION="${pkgver}"
PACKAGE_CONFIG="/etc/sysconfig/lustre"
# re-evaluate/create dkms.conf on-target based on local build-time products/dependencies
# (like krb5-devel presence) and also upon configure results/errors before dkms builds
PRE_BUILD="lustre-dkms_pre-build.sh \$module \$module_version \$kernelver \$kernel_source_dir \
	 \$arch \$source_tree \$dkms_tree"
# to ensure all just built Lustre kernel modules will be saved for further install
POST_BUILD="lustre-dkms_post-build.sh \$module \$module_version \$kernelver \$kernel_source_dir \
	 \$arch \$source_tree \$dkms_tree"
EOF
if [ $pkgname = "lustre-zfs" -o $pkgname = "lustre-all" ] ; then
	cat >>${filename} <<EOF
BUILD_DEPENDS[0]="zfs"
EOF
fi
cat >>${filename} <<EOF
AUTOINSTALL="yes"
REMAKE_INITRD="no"
MAKE[0]="make"
# just have to set STRIP[0], it will become the new default.
STRIP[0]="\$(
  [[ -r \${PACKAGE_CONFIG} ]] \\
  && source \${PACKAGE_CONFIG} \\
  && shopt -q -s extglob \\
  && [[ \${LUSTRE_DKMS_DISABLE_STRIP,,} == @(y|yes) ]] \\
  && echo -n no
)"

# Common modules for both Client & Server
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="lnet_selftest"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lnet/selftest/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lnet/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="lnet"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lnet/lnet/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lnet/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="ksocklnd"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lnet/klnds/socklnd/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lnet/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="ko2iblnd"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lnet/klnds/o2iblnd/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lnet/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="libcfs"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="libcfs/libcfs/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="ptlrpc"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/ptlrpc/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="lov"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/lov/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="fld"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/fld/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="obdecho"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/obdecho/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="osc"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/osc/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="mgc"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/mgc/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="fid"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/fid/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="mdc"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/mdc/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="obdclass"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/obdclass/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="lmv"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/lmv/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="lustre"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/llite/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
if \$( rpm -qa | grep -q krb5-devel ) ; then
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="ptlrpc_gss"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/ptlrpc/gss/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
fi
EOF
if [ $pkgname != "lustre-client" ] ; then
	cat >>${filename} <<EOF
# Server Modules
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="ofd"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/ofd/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="ost"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/ost/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="lfsck"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/lfsck/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="mgs"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/mgs/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="lquota"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/quota/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="mdt"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/mdt/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="osp"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/osp/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="mdd"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/mdd/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="llog_test"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/obdclass/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="obd_test"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/obdclass/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="lod"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/lod/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
EOF
fi
if [ $pkgname = "lustre-zfs" -o $pkgname = "lustre-all" ] ; then
	cat >>${filename} <<EOF
# Module for ZFS backing
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="osd_zfs"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/osd-zfs/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
EOF
fi
if [ $pkgname = "lustre-ldiskfs" -o $pkgname = "lustre-all" ] ; then
	cat >>${filename} <<EOF
# Module for ldiskfs backing
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="osd_ldiskfs"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="lustre/osd-ldiskfs/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
# ldiskfs module
BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="ldiskfs"
BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="ldiskfs/"
DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/lustre/"
EOF
fi
